package com.sugar.psych.assessment.module.business.controller.admin.psychquestion;

import com.sugar.psych.assessment.module.infra.controller.app.file.vo.AppFileUploadReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.psych.assessment.framework.common.pojo.PageParam;
import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.framework.common.pojo.CommonResult;
import com.sugar.psych.assessment.framework.common.util.object.BeanUtils;
import static com.sugar.psych.assessment.framework.common.pojo.CommonResult.success;

import com.sugar.psych.assessment.framework.excel.core.util.ExcelUtils;

import com.sugar.psych.assessment.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.psych.assessment.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.psych.assessment.module.business.controller.admin.psychquestion.vo.*;
import com.sugar.psych.assessment.module.business.dal.dataobject.psychquestion.PsychQuestionDO;
import com.sugar.psych.assessment.module.business.service.psychquestion.PsychQuestionService;

@Tag(name = "管理后台 - 心理测评题目")
@RestController
@RequestMapping("/business/psych-question")
@Validated
public class PsychQuestionController {

    @Resource
    private PsychQuestionService psychQuestionService;

    @PostMapping("/create")
    @Operation(summary = "创建心理测评题目")
    @PreAuthorize("@ss.hasPermission('business:psych-question:create')")
    public CommonResult<Long> createPsychQuestion(@Valid @RequestBody PsychQuestionSaveReqVO createReqVO) {
        return success(psychQuestionService.createPsychQuestion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新心理测评题目")
    @PreAuthorize("@ss.hasPermission('business:psych-question:update')")
    public CommonResult<Boolean> updatePsychQuestion(@Valid @RequestBody PsychQuestionSaveReqVO updateReqVO) {
        psychQuestionService.updatePsychQuestion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除心理测评题目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:psych-question:delete')")
    public CommonResult<Boolean> deletePsychQuestion(@RequestParam("id") Long id) {
        psychQuestionService.deletePsychQuestion(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除心理测评题目")
                @PreAuthorize("@ss.hasPermission('business:psych-question:delete')")
    public CommonResult<Boolean> deletePsychQuestionList(@RequestParam("ids") List<Long> ids) {
        psychQuestionService.deletePsychQuestionListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得心理测评题目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:psych-question:query')")
    public CommonResult<PsychQuestionRespVO> getPsychQuestion(@RequestParam("id") Long id) {
        PsychQuestionDO psychQuestion = psychQuestionService.getPsychQuestion(id);
        return success(BeanUtils.toBean(psychQuestion, PsychQuestionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得心理测评题目分页")
    @PreAuthorize("@ss.hasPermission('business:psych-question:query')")
    public CommonResult<PageResult<PsychQuestionRespVO>> getPsychQuestionPage(@Valid PsychQuestionPageReqVO pageReqVO) {
        return success(psychQuestionService.getPsychQuestionPage(pageReqVO));
    }

    @PostMapping("/import-excel")
    @Operation(summary = "导入心理测评题目 Excel")
    @PreAuthorize("@ss.hasPermission('business:psych-question:export')")
    @ApiAccessLog(operateType = EXPORT)
    public CommonResult<Boolean> importPsychQuestionExcel(AppFileUploadReqVO reqVO) throws IOException {
        psychQuestionService.importExcel(reqVO);
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出心理测评题目 Excel")
    @PreAuthorize("@ss.hasPermission('business:psych-question:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPsychQuestionExcel(@Valid PsychQuestionPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        psychQuestionService.writeExcel(pageReqVO, response);
    }
}